import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**GUI is the main runner class for the Circle Game.
 * All parts of the game are instantiated and managed
 * in this class.
 * 
 * @author Rayten Rex and Brayden McCoy
 *
 */
public class GUI extends JFrame
{
	/**Constant Dimension used to manage the sizes of each
	 * aspect of the GUI.
	 */
	private static final Dimension SIZE = new Dimension(600, 600);
	
	private DrawCircles circlePanel;
	private TimerMan timer;
	private JTextField input;
	private ButtonListener button;
	private int circles;
	
	public GUI()
	{
		super("Circle Game");
		this.setSize(GUI.SIZE);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setLayout(new BorderLayout());
		Container c = this.getContentPane();
		c.setBackground(Color.BLACK);
		
		Counter count = new Counter((int)(Math.random() * 20) + 1);
		
		this.circles = count.getValue();
		
		JPanel bottomJPanel = new JPanel();
		bottomJPanel.setBackground(Color.LIGHT_GRAY);
		bottomJPanel.setSize(new Dimension(GUI.SIZE.width, 50));
		this.add(bottomJPanel, BorderLayout.SOUTH);
		
		this.circlePanel = new DrawCircles(new Dimension(GUI.SIZE.width, GUI.SIZE.height - bottomJPanel.getHeight()), this.circles);
		this.add(this.circlePanel, BorderLayout.NORTH);
		
		int time = 0;
		
		switch(this.circles)
		{
		case 20:
		case 19:
		case 18:
			time++;
			time++;
		case 17:
		case 16:
		case 15:
			time++;
		case 14:
		case 13:
		case 12:
			time++;
		case 11:
		case 10:
		case 9:
			time++;
		case 8:
		case 7:
		case 6:
			time++;
		default:
			time += 4;
		}
		
		bottomJPanel.setLayout(new BorderLayout());
		this.timer = new TimerMan(new Dimension(50, bottomJPanel.getHeight()), time);
		bottomJPanel.add(this.timer, BorderLayout.EAST);
		
		this.input = new JTextField();
		this.input.setPreferredSize(new Dimension(80, bottomJPanel.getHeight()));
		Font inputFont = new Font("Sans-Serif", Font.PLAIN, bottomJPanel.getHeight() - 5);
		this.input.setFont(inputFont);
		bottomJPanel.add(this.input, BorderLayout.WEST);
		
		this.button = new ButtonListener(this.input, this.circles, new Dimension(this.WIDTH - this.input.WIDTH - this.timer.WIDTH, bottomJPanel.getHeight()), this.timer);
		bottomJPanel.add(this.button, BorderLayout.CENTER);

		this.pack();
		this.setVisible(true);
	}
	
	public boolean run()
	{
		if(this.button.hasGuessed() || !this.timer.getTimer().isRunning())
		{
			if(!this.timer.getTimer().isRunning())
				JOptionPane.showMessageDialog(null, "You ran out of time");
			
			return false;
		}
		
		return true;
	}
	
	public static void main(String [] args)
	{
		GUI runner = new GUI();
		
		while(runner.run());
		
		System.exit(EXIT_ON_CLOSE);
	}
}
